package com.example.android_gps01

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener

class MainActivity : AppCompatActivity() {
    //声明AMapLocationClient类对象
    var mLocationClient: AMapLocationClient? = null

    //声明定位回调监听器
    var mLocationListener: AMapLocationListener? = null

    //声明AMapLocationClientOption对象
    var mLocationOption: AMapLocationClientOption? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        //1、测试获取当前位置
        val button = findViewById<View>(R.id.button) as Button
        button.setOnClickListener {
            getGPSInfo()
            Log.d("onClick", "onClick")
        }
    }

    private fun getGPSInfo(){
        mLocationClient = AMapLocationClient(applicationContext)
        mLocationOption = AMapLocationClientOption()
        mLocationOption!!.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.Transport)
        if (null != mLocationClient) {
            mLocationClient!!.setLocationOption(mLocationOption)
            //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
            mLocationClient!!.stopLocation()
            mLocationClient!!.startLocation()
        }
        //设置定位模式为AMapLocationMode.Device_Sensors，仅设备模式。
        mLocationOption!!.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy
        //设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
        mLocationOption!!.interval = 1000
        //给定位客户端对象设置定位参数
        mLocationClient!!.setLocationOption(mLocationOption)
        mLocationOption!!.isMockEnable = true
        mLocationListener = AMapLocationListener { aMapLocation ->
            if (aMapLocation != null) {
                if (aMapLocation.errorCode == 0) {
                    //可在其中解析amapLocation获取相应内容。
                    val t1 = findViewById<View>(R.id.text) as TextView
                    t1.text = "纬度：${aMapLocation.latitude} 经度：${aMapLocation.longitude}"
                    Log.d("定位成功", "定位成功")
                } else {
                    //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                    Log.e("AmapError", "location Error, ErrCode:"
                            + aMapLocation.errorCode + ", errInfo:"
                            + aMapLocation.errorInfo)
                    Toast.makeText(applicationContext, "定位失败", Toast.LENGTH_LONG)
                }
            }
        }
        mLocationClient!!.setLocationListener(mLocationListener)

        //启动定位
        mLocationClient!!.startLocation()
        Log.d("getGPSInfo", "getGPSInfo")
    }

    override fun onDestroy() {
        mLocationClient!!.stopLocation() //停止定位后，本地定位服务并不会被销毁
        mLocationClient!!.onDestroy() //销毁定位客户端，同时销毁本地定位服务
        super.onDestroy()
    }
}